package com.example.manage.controller;

import com.example.manage.Service.FindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class ManageController {

    private final FindService findService;

    @Autowired
    public ManageController(FindService findService) {
        this.findService = findService;
    }

    /**
     * 获取简历信息接口
     * 请求路径：GET /api/find-resume
     * 请求头需要包含 X-User-ID
     */
    @GetMapping("/find-resume")
    public String findResume(@RequestHeader("X-User-ID") String userid) {
        return findService.findResume(userid);
    }

    /**
     * 获取已完成面试信息接口
     * 请求路径：GET /api/find-interview
     * 请求头需要包含 X-User-ID
     */
    @GetMapping("/find-interview")
    public String findAllInterview(@RequestHeader("X-User-ID") String userid) {
        return findService.findAllInterview(userid);
    }

    /**
     * 删除指定用户的 Redis 缓存
     * X-Cache-Type: 0=简历, 1=已完成面试
     */
    @DeleteMapping("/clear-cache")
    public String clearCache(
            @RequestHeader("X-User-ID") String userid,
            @RequestHeader("X-Cache-Type") int cacheType) {
        try {
            findService.deleteRedis(userid, cacheType);
            return "{\"status\":\"success\",\"message\":\"缓存已清除\"}";
        } catch (IllegalArgumentException e) {
            return "{\"status\":\"error\",\"message\":\"" + e.getMessage() + "\"}";
        } catch (Exception e) {
            return "{\"status\":\"error\",\"message\":\"内部错误：" + e.getMessage() + "\"}";
        }
    }

    @GetMapping("/interview-result/{interview_id}")
    public String interviewResult(@PathVariable("interview_id") String interviewId) {
        String summary = findService.interviewResult(interviewId);
        if (summary != null) {
            return summary;
        } else {
            return "null";
        }
    }
}
